package com.atguigu.bigdata.scala.mytest.chapter07

object Test_WordCount3 {
  def main(args: Array[String]): Unit = {
    val list = List(
      ("hello", 4),
      ("hello spark", 3),
      ("hello spark scala", 2),
      ("hello spark scala hive", 1)
    )

    val list1: List[Array[(String, Int)]] = list.map(w => {
      val strings: Array[String] = w._1.split(" ")
      strings.map(s => (s, w._2))
    })

    val flatten: List[(String, Int)] = list1.flatten
    println(flatten)

    val stringToTuples: Map[String, List[(String, Int)]] = flatten.groupBy(w => w._1)
    println(stringToTuples)

    val stringToInt: Map[String, Int] = stringToTuples.map(w => {
      val sum: Int = w._2.map(t => t._2).sum
      (w._1, sum)
    })
    println(stringToInt)

    val list2: List[(String, Int)] = stringToInt.toList
    println(list2)

    val tuples: List[(String, Int)] = list2.sortBy(w => w._2)(Ordering.Int.reverse).take(3)
    println(tuples)



  }
}
